﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Assessment.Management.SharedComponent.Entities;
using Assessment.Management.SharedComponent.Constants;
using Assessment.Management.SharedComponent.Params.ManagerParam;
using Assessment.Management.BIZ;
using System.Web.UI.HtmlControls;
using Assessment.Management.SharedComponent.Params.MenuManagers;

namespace Assessment.Management.UI.Admin.UserControls.Permissions
{
    public partial class PermissionManager : System.Web.UI.UserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadGridView();
            }
        }

        protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkAll = (CheckBox)grvPermission.HeaderRow.FindControl("chkSelectAll");
            foreach (GridViewRow gvRow in grvPermission.Rows)
            {
                CheckBox chkSel = (CheckBox)grvPermission.FindControl("chkSelect");
                chkSel.Checked = chkAll.Checked;
            }
        }

        protected void grvPermission_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Account acc = (Account)e.Row.DataItem;
                HiddenField hidID = (HiddenField)e.Row.FindControl("hidID");
                hidID.Value = acc.Username.ToString();
                HiddenField hidVersion = (HiddenField)e.Row.FindControl("hidVersion");
                hidVersion.Value = acc.Version.ToString();

                HyperLink hypEdit = (HyperLink)e.Row.FindControl("hypEdit");
                hypEdit.NavigateUrl = string.Format(SMX.smx_PermissionEdit, acc.Username);

                ImageButton btnDelete = (ImageButton)e.Row.FindControl("btnDelete");
                string msg = string.Format("Bạn có chắc chắn muốn xóa tài khoản có tên \"{0}\"?", acc.Username);
                btnDelete.CommandName = "Delete";
                btnDelete.OnClientClick = string.Format("return confirm('{0}');", msg);
            }
        }
        //Sự kiện xóa dòng
        protected void grvPermission_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row = grvPermission.Rows[e.RowIndex];
            HiddenField hidID = (HiddenField)row.FindControl("hidID");
            HiddenField hidVersion = (HiddenField)row.FindControl("hidVersion");
            List<Account> lstcl = new List<Account>();
            lstcl.Add(new Account()

            {
                Username = hidID.Value.ToString(),
                Version = int.Parse(hidVersion.Value),
                Deleted = SMX.smx_IsDeleted,
                UpdatedBy = SMX.smx_UpdatedBy,
                UpdatedDTG = DateTime.Now
            });
            DeletePermission(lstcl);
            LoadGridView();
        }
        //Sự kiện chuyển trang
        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            LoadGridView();
            grvPermission.PageIndex = e.NewPageIndex;
            grvPermission.DataBind();
        }
        //#endregion
        //#region methods

        //Lấy dữ liệu đổ lên gridview
        private void LoadGridView()
        {
            AccountParam param = new AccountParam(FunctionType.GetPermission);
            MainController.Provider.Execute(param);
            grvPermission.DataSource = param.listAccount;
            grvPermission.DataBind();
           hypAddNew.HRef = SMX.smx_PermissionAdd;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<Account> lstcl = GetSelectedItemOnGrid();
                DeletePermission(lstcl);
                //Load lại gridview
                LoadGridView();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }

        //Lấy dữ liệu trên Gridview
        private Account GetItemOnGrid(GridViewRow item)
        {
            Account result = new Account();
            HiddenField hidId = (HiddenField)item.FindControl("hidID");
            HiddenField hidVersion = (HiddenField)item.FindControl("hidVersion");
            result.Username = hidId.Value;
            result.Version = int.Parse(hidVersion.Value);
            result.Deleted = SMX.smx_IsDeleted;
            result.UpdatedBy = SMX.smx_UpdatedBy;
            result.UpdatedDTG = DateTime.Now;
            return result;
        }

        //Lây dữ liệu các dòng được check trên Gridview
        private List<Account> GetSelectedItemOnGrid()
        {
            List<Account> list = new List<Account>();

            foreach (GridViewRow gvRow in grvPermission.Rows)
            {
                CheckBox ckSelected = (CheckBox)gvRow.FindControl("chkSelect");
                if (ckSelected.Checked)
                {
                    list.Add(GetItemOnGrid(gvRow));
                }
            }
            return list;
        }

        protected void btnExit_Click(object sender, EventArgs e)
        {
            Response.Redirect("Dashboard.aspx");
        }

        private void DeletePermission(List<Account> lstcl)
        {
            AccountParam param = new AccountParam(FunctionType.DeleteAccount);
            param.listAccount = lstcl;
            MainController.Provider.Execute(param);
        }

    }
}